package controller;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import model.Hunter;
import model.Simulator;

public class ScoreboardController extends JFrame implements ActionListener, ListSelectionListener 
{
	private static final long serialVersionUID = 5565308400237513102L;

	// ScoreboardView's elements 
	private JPanel contentPane;
	private JButton btnClose;
	private JLabel lblHunterScore;
	private JLabel lblHunterName;
    //private int score;
	private double TotalScore;
    private JLabel lblHunterAverage;
	
    private Simulator simulator;

	private JButton btnNewHunter;
    
	public ScoreboardController(Simulator simulator)
	{
    	// Simulator
    	this.simulator = simulator;
    	
		// Make the window frame
		createView();
		
		// Set the total score
		setTotalScore();
	}
	
	/**
	 * Make the window frame
	 */
	@SuppressWarnings("rawtypes")
	protected void createView()
	{
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		btnNewHunter = new JButton("New hunter");
		btnNewHunter.addActionListener(this);
		btnNewHunter.setBounds(225, 182, 100, 23);
		contentPane.add(btnNewHunter);
		
		btnClose = new JButton("Close");
		btnClose.addActionListener(this);
		btnClose.setBounds(332, 182, 89, 23);
		contentPane.add(btnClose);
		
		JList listHunters = addHuntersToList();
		listHunters.setBounds(212, 37, 209, 133);
		contentPane.add(listHunters);
		
		JLabel lblName = new JLabel("Name: ");
		lblName.setBounds(10, 37, 46, 14);
		contentPane.add(lblName);
		
		lblHunterName = new JLabel();
		lblHunterName.setBounds(150, 37, 46, 14);
		contentPane.add(lblHunterName);
		
		JLabel lblScoreboard = new JLabel("Scoreboard");
		lblScoreboard.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblScoreboard.setBounds(10, 12, 127, 14);
		contentPane.add(lblScoreboard);
		
		JLabel lblKills = new JLabel("Kills:");
		lblKills.setBounds(10, 62, 46, 14);
		contentPane.add(lblKills);
		
		lblHunterScore = new JLabel();
		lblHunterScore.setBounds(150, 62, 46, 14);
		contentPane.add(lblHunterScore);
		
		JLabel lblAveragePercentage = new JLabel("Average percentage:");
		lblAveragePercentage.setBounds(10, 87, 103, 14);
		contentPane.add(lblAveragePercentage);
		
		lblHunterAverage = new JLabel();
		lblHunterAverage.setBounds(150, 87, 103, 14);
		contentPane.add(lblHunterAverage);
		
		JLabel lblHunters = new JLabel("Hunters");
		lblHunters.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblHunters.setBounds(212, 13, 78, 14);
		contentPane.add(lblHunters);
	}
	
	@SuppressWarnings({"rawtypes", "unchecked"})
	private JList addHuntersToList()
	{
		DefaultListModel listModel = new DefaultListModel();

		for(Iterator<Hunter> hunter = simulator.getHunters().iterator(); hunter.hasNext(); )
		{
			Hunter h = hunter.next();
			listModel.addElement(h.name);
		}
	
		JList listHunters = new JList(listModel);
		listHunters.addListSelectionListener(this);
		
		return listHunters;
	}

	@Override
	public void actionPerformed(ActionEvent action) 
	{
		if(action.getSource().equals(btnClose))
		{
			dispose();
		}
		if(action.getSource().equals(btnNewHunter))
		{
			new HunterController(simulator);
		}
	}

	public void setTotalScore()
	{
		ArrayList<Hunter> hunterlist = simulator.getHunters();
		for(Hunter hunter : hunterlist)
		{
			this.TotalScore += hunter.getTotalScore();
		}
	}
	
	public void setTotalHunterScore(Hunter hunter)
	{
		if(TotalScore > 0)
		{
			double hunterTotal = (double) hunter.getTotalScore();
			double hunterAverage = (hunterTotal / this.TotalScore) * 100;
		    lblHunterAverage.setText(Integer.toString((int)hunterAverage) + "%");
		}
		else
		{
			lblHunterAverage.setText("-");
		}
			
	}
	
	public void setHunterScore(Hunter hunter)
	{
		lblHunterScore.setText(Integer.toString(hunter.getTotalScore()) + " / " + (int)TotalScore);
	}

	public void setHunterName(Hunter hunter)
	{
		lblHunterName.setText(hunter.name);
	}

	@Override
	public void valueChanged(ListSelectionEvent e) 
	{
		@SuppressWarnings("rawtypes")
		JList list = (JList) e.getSource();
		ArrayList<Hunter> hunterlist = simulator.getHunters();
		for(Hunter hunter : hunterlist)
		{
			if(list.getSelectedValue().equals(hunter.name))
			{
				setHunterScore(hunter);
				setHunterName(hunter);
				setTotalHunterScore(hunter);
			}
		}
	}
}
